/**
 * 微信公众平台开发模式(JAVA) SDK
 * (c) 2012-2013 ____′↘夏悸 <wmails@126.cn>, MIT Licensed
 * http://www.jeasyuicn.com/wechat
 */
package com.zzld.modules.wechat.inf;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.zzld.modules.autoinn.service.ICollarService;
import com.zzld.modules.autoinn.service.IConponService;
import com.zzld.modules.wechat.WeChat;
import com.zzld.modules.wechat.bean.Articles;
import com.zzld.modules.wechat.bean.InMessage;
import com.zzld.modules.wechat.bean.NewsOutMessage;
import com.zzld.modules.wechat.bean.OutMessage;
import com.zzld.modules.wechat.bean.TextOutMessage;
import com.zzld.modules.wechat.oauth.Menu;
import com.zzld.modules.wechat.oauth.Oauth;

public class DefaultMessageProcessingHandlerImpl implements
		MessageProcessingHandler {

	private OutMessage outMessage;

	@Autowired
	private ICollarService collarService;

	@Autowired
	private IConponService couponService;

	@Override
	public void allType(InMessage msg) {
		TextOutMessage out = new TextOutMessage();
		out.setContent("您的消息已经收到！");
		setOutMessage(out);
	}

	@Override
	public void textTypeMsg(InMessage msg) {
		Oauth oauthInfo = new Oauth();

		if (msg.getContent().equals("包包") || msg.getContent().equals("bb")) {
			String url = "";
			try {
				url = oauthInfo.getCode("baozhu");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			TextOutMessage out = new TextOutMessage();
			url = "<a href='" + url + "'>到店洗车</a>";
			out.setContent(url);
			setOutMessage(out);

			return;
		}
		
		

		if (msg.getContent().equals("创建菜单")) {
			TextOutMessage out = new TextOutMessage();
			String accessToken;
			try {
				accessToken = WeChat.getAccessToken();
				Menu menu = new Menu();
				/*
				 * boolean istrue=menu.createMenu(accessToken,
				 * "{\"button\": [ { \"type\": \"view\"," +
				 * " \"name\": \"到店洗车\", \"key\":\"xc\", " +
				 * "  \"url\":\""+oauthInfo.getCode("storelist")+"\"}," +
				 * "  {  \"name\": \"用户中心\", \"type\": \"view\"," +
				 * " \"key\": \"userinfo\"," +
				 * " \"url\":\""+oauthInfo.getCode()+"\"}" +"]}");
				 */

				boolean istrue=menu.createMenu(accessToken,"{\"button\":["
				        + " {\"type\":\"view\",\"name\":\"到店洗车\","
				        + " \"url\":\""+oauthInfo.getCode("storelist")+"\""
				        + " }"
			            + ",{\"type\":\"view\",\"name\":\"首页\","
				        + " \"url\":\""+oauthInfo.getCode("baozhu")+"\""
				        + " },{ \"name\":\"我的\", \"sub_button\":[{\"type\":\"view\",\"name\":\"违章查询\",\"url\":\"http://m.weizhang8.cn/\"},"
				        + "{\"type\":\"view\",\"name\":\"洗车指数\",\"url\":\"http://m.weather.com.cn/mzs/101170101.shtml?flag=xc\"},"
				        + "{\"type\":\"view\",\"name\":\"商户登陆\",\"url\":\""+oauthInfo.getCode("shlist")+"\"},"
				        + "{\"type\":\"view\",\"name\":\"消费记录\",\"url\":\""+oauthInfo.getCode("dinglist")+"\"},"
			            + "{\"type\":\"view\",\"name\":\"用户中心\",\"url\":\""+oauthInfo.getCode()+"\" }]}]}");


				if (istrue) {
					out.setContent("菜单创建成功");
					setOutMessage(out);
					return;
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			out.setContent("菜单创建失败");
			setOutMessage(out);
			return;
		} else if (msg.getContent().equals("门票")) {
			TextOutMessage out = new TextOutMessage();
			out.setContent("http://auto.nxsh114.com/zzld/bao/index");
			setOutMessage(out);

			return;
		} else if (msg.getContent().equals("拍汽车")) {
			 NewsOutMessage out1 = new NewsOutMessage();
			 out1.setUrl("http://auto.nxsh114.com/paicar/index.html");
			 out1.setDescription("拍汽车");
			 out1.setPicUrl("http://auto.nxsh114.com/zzld/static/images/paicar.jpg");
			 Articles a = new Articles();
			 a.setDescription("拍汽车");
			 a.setUrl("http://auto.nxsh114.com/paicar/index.html");
			 a.setTitle("拍汽车");
			 a.setPicUrl("http://auto.nxsh114.com/zzld/static/images/paicar.jpg");
			 List<Articles> list = new ArrayList<Articles>();
			 list.add(a);
			
			 out1.setArticles(list);
			 out1.setTitle("拍汽车");
		     
			setOutMessage(out1);
			return;
		}else if(msg.getContent().equals("保养")){
			 NewsOutMessage out2 = new NewsOutMessage();
			 try {
				out2.setUrl(oauthInfo.getCode("bylist"));
			} catch (UnsupportedEncodingException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			 out2.setDescription("保养");
			 out2.setPicUrl("http://auto.nxsh114.com/zzld/static/images/paicar.jpg");
			 Articles ab = new Articles();
			 ab.setDescription("保养");
			 try {
					ab.setUrl(oauthInfo.getCode("bylist"));
				} catch (UnsupportedEncodingException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
			 ab.setTitle("保养");
			 ab.setPicUrl("http://auto.nxsh114.com/zzld/static/images/paicar.jpg");
			 List<Articles> list = new ArrayList<Articles>();
			 list.add(ab);
			
			 out2.setArticles(list);
			 out2.setTitle("拍汽车");
		     
			setOutMessage(out2);
			return;
		}else if(msg.getContent().equals("保险")){
			try{
			 NewsOutMessage out3 = new NewsOutMessage();
			 out3.setUrl(oauthInfo.getCode("baoxian"));
			 out3.setDescription("冰桶挑战");
			 
			 Articles ac = new Articles();
			 ac.setDescription("冰桶挑战");
			 ac.setUrl(oauthInfo.getCode("baoxian"));
			 ac.setTitle("我的保险");
			
			 List<Articles> list = new ArrayList<Articles>();
			 list.add(ac);
			
			 out3.setArticles(list);
			 out3.setTitle("我的保险");
		     
			setOutMessage(out3);
			}catch(Exception e){
				e.printStackTrace();
			}
			return;
		}else if(msg.getContent().equals("商户")){
			 NewsOutMessage out10 = new NewsOutMessage();
			 try {
				out10.setUrl(oauthInfo.getCode("shlist"));
			} catch (UnsupportedEncodingException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			 out10.setDescription("商户");
			 
			 Articles ab = new Articles();
			 ab.setDescription("商户");
			 try {
					ab.setUrl(oauthInfo.getCode("shlist"));
				} catch (UnsupportedEncodingException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
			 ab.setTitle("商户");
			
			 List<Articles> list = new ArrayList<Articles>();
			 list.add(ab);
			
			 out10.setArticles(list);
			 out10.setTitle("商户");
		     
			setOutMessage(out10);
			return;
		}

		try {
			String url = oauthInfo.getCode();
			url = "感谢您关注【宁夏生活汇-E爱车】,<a href='" + url + "'>绑定后使用</a>";
			TextOutMessage out = new TextOutMessage();
			out.setContent(url);
			setOutMessage(out);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void locationTypeMsg(InMessage msg) {
	}

	@Override
	public void imageTypeMsg(InMessage msg) {
		
	}

	@Override
	public void videoTypeMsg(InMessage msg) {
	}

	@Override
	public void voiceTypeMsg(InMessage msg) {
	}

	@Override
	public void linkTypeMsg(InMessage msg) {
	}

	@Override
	public void verifyTypeMsg(InMessage msg) {

	}

	@Override
	public void eventTypeMsg(InMessage msg) {
		// 事件类型
		String eventType = msg.getEvent();
		// 触发订阅事件
		if (eventType.equals("subscribe")) {
			try {
				Oauth oauthInfo = new Oauth();
				String url = oauthInfo.getCode();
				TextOutMessage out = new TextOutMessage();
				url = "感谢您关注【宁夏生活汇-E爱车】,<a href='" + url + "'>绑定后使用</a>";

				out.setContent(url);
				setOutMessage(out);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else if (eventType.equals("CLICK")) {// 自定义菜单点击事件

		}

	}

	@Override
	public void setOutMessage(OutMessage outMessage) {
		this.outMessage = outMessage;
	}

	@Override
	public void afterProcess(InMessage inMessage, OutMessage outMessage) {
	}

	@Override
	public OutMessage getOutMessage() {
		return outMessage;
	}

}
